<template>
  <div>
    <el-input v-model="userVal" placeholder="请输入内容"></el-input>
    <el-table :data="tableData">
      <el-table-column
        sortable
        prop="id"
        label="ID"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="username"
        label="用户名字"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="password"
        label="密码"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="nickname"
        label="昵称"
        width="100"
      ></el-table-column>
      <el-table-column prop="avatar" label="头像" width="100">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.avatar"
            style="width: 50px; height: 50px"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column prop="gender" label="性别" width="100"></el-table-column>
      <el-table-column
        sortable
        prop="createTime"
        label="ID"
        width="100"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :page-size="params.size"
      layout="prev, pager, next"
      :total="total"
      @current-change="onChange"
    ></el-pagination>
  </div>
</template>

<script>
import { getUserList, getUserSearch } from '@/api'
import moment from 'moment'
import { debounce } from 'lodash'
export default {
  data() {
    return {
      tableData: [],
      params: {
        page: 1,
        size: 5,
      },
      total: 0,
      userVal: '',
    }
  },
  watch: {
    userVal() {
      this.fetchSearchUsername()
    },
  },
  created() {
    this.fetchUserList()
  },
  methods: {
    handleEdit() {},
    handleDelete() {},
    fetchSearchUsername: debounce(async function () {
      console.log(this.userVal, '------')
      const res = await getUserSearch({ params: { keywords: this.userVal } })
      console.log(res.data.data, 'res')
      this.tableData = res.data.data
    }, 500),
    onChange(page) {
      this.params.page = page
      this.fetchUserList()
    },
    async fetchUserList() {
      const res = await getUserList({ params: this.params })
      console.log(res.data.data.userData, 'res')
      const data = res.data.data
      this.total = data.total
      console.log('data:', data)
      data.userData.forEach((item) => {
        item.createTime = moment(item.createTime).format('YYYY-MM-DD')
      })

      this.tableData = data.userData
    },
  },
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
